﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;

namespace S7.Windows.Customers
{
    /// <summary>
    /// Interaction logic for EditCustomers.xaml
    /// </summary>
    public partial class EditCustomers : Window
    {
        public EditCustomers()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Thread thr = new Thread(new ThreadStart(() =>
            {
                try
                {
                    Customer.lstCustomers = Customer.Search().ToList();
                    lbCustomers.Dispatcher.Invoke(new ThreadStart(() =>
                    {
                        lbCustomers.ItemsSource = Customer.lstCustomers;
                    }), null);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                }
            }));
            thr.IsBackground = true;
            thr.Start();
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            var w = new AddAnAccount();
            w.ShowDialog();
            Customer.lstCustomers = Customer.Search().ToList();
            Search();
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            if (lbCustomers.SelectedItem != null)
            {
                string Name = (lbCustomers.SelectedValue as Customer).UserName;
                Thread thr = new Thread(new ThreadStart(() =>
                {
                    try
                    {
                        DataAccessDataContext obj = new DataAccessDataContext();
                        var customers = from cust in Customer.lstCustomers
                                        where cust.UserName == Name
                                        select cust;
                        List<Customer> selectedCustomers = customers.ToList();
                        Customer.curCustomerID = selectedCustomers[0].CustomerID;
                        Application.Current.Dispatcher.Invoke(new ThreadStart(() =>
                        {
                            var w = new EditAnAccount();
                            w.ShowDialog();
                            Customer.lstCustomers = Customer.Search().ToList();
                            Search();
                        }), null);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                    }
                }));
                thr.IsBackground = true;
                thr.Start();
            }
            else
            {
                MessageBox.Show("Please select a customer", "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void btnRemove_Click(object sender, RoutedEventArgs e)
        {
            if (lbCustomers.SelectedValue != null)
            {
                string Name = (lbCustomers.SelectedValue as Customer).UserName;
                Thread thr = new Thread(new ThreadStart(() =>
                {
                    try
                    {
                        DataAccessDataContext obj = new DataAccessDataContext();
                        var customers = from cust in Customer.lstCustomers
                                        where cust.UserName == Name
                                        select cust;
                        List<Customer> selectedCustomers = customers.ToList();
                        string error = selectedCustomers[0].DeleteByManager();
                        if (error == "")
                        {
                            MessageBox.Show("Customer is deleted", "Done!", MessageBoxButton.OK, MessageBoxImage.Information);
                            Customer.lstCustomers = Customer.Search().ToList();
                            Search();
                        }
                        else
                        {
                            MessageBox.Show(error, "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                }));
                thr.IsBackground = true;
                thr.Start();
            }
            else
            {
                MessageBox.Show("Please select a customer", "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Customer.lstCustomers = null;
            Customer.curCustomerID = null;
        }

        private void tbSearch_KeyUp(object sender, KeyEventArgs e)
        {
            Search();
        }

        private void Search()
        {
            string text = tbSearch.Text;
            if (text == "")
            {
                lbCustomers.ItemsSource = Customer.lstCustomers;
            }
            else
            {
                Thread thrSearch = new Thread(new ThreadStart(() =>
                {
                    while (true)
                    {
                        if (Customer.lstCustomers == null)
                        {
                            Thread.Sleep(50);
                        }
                        else
                        {
                            List<Customer> lstItems = Customer.SearchText(text);
                            lbCustomers.Dispatcher.Invoke(new ThreadStart(() =>
                            {
                                lbCustomers.ItemsSource = lstItems;
                            }), null);
                            break;
                        }
                    }
                }));
                thrSearch.IsBackground = true;
                thrSearch.Start();
            }
        }
    }
}
